package com.zzyl.nursing.service.impl;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.common.utils.UserThreadLocal;
import net.bytebuddy.implementation.bytecode.Throw;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ReservationMapper;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

/**
 * 预约信息Service业务层处理
 *
 * @author Lorine
 * @date 2025-03-05
 */
@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements IReservationService {
    @Autowired
    private ReservationMapper reservationMapper;

    /**
     * 查询预约信息
     *
     * @param id 预约信息主键
     * @return 预约信息
     */
    @Override
    public Reservation selectReservationById(Long id) {
        return reservationMapper.selectById(id);
    }

    /**
     * 查询预约信息列表
     *
     * @param reservation 预约信息
     * @return 预约信息
     */
    @Override
    public List<Reservation> selectReservationList(Reservation reservation) {
        reservation.setCreateBy(UserThreadLocal.get().toString());
        return reservationMapper.selectReservationList(reservation);
    }

    /**
     * 新增预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int insertReservation(Reservation reservation) {
        reservation.setStatus(0);
        reservation.setCreateBy(UserThreadLocal.get().toString());
        return reservationMapper.insert(reservation);
    }

    /**
     * 修改预约信息
     *
     * @param reservation 预约信息
     * @return 结果
     */
    @Override
    public int updateReservation(Reservation reservation) {
        return reservationMapper.updateById(reservation);
    }

    /**
     * 批量删除预约信息
     *
     * @param ids 需要删除的预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationByIds(Long[] ids) {
        return reservationMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除预约信息信息
     *
     * @param id 预约信息主键
     * @return 结果
     */
    @Override
    public int deleteReservationById(Long id) {
        return reservationMapper.deleteById(id);
    }

    @Override
    public Long getCancelledCount() {
        Long userId = UserThreadLocal.get();
        return reservationMapper.getCancelledCount(userId);
    }

    /**
     * 查询每个时间段剩余预约次数
     *
     * @param time
     * @return
     */
    @Override
    public List<Map<String, Object>> countByTime(Long time) {
        LocalDateTime dateTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(time), ZoneId.of("Asia/Shanghai"));
        return reservationMapper.countByTime(dateTime);
    }

    @Override
    public int cancel(Long id) {
        Reservation reservation = reservationMapper.selectById(id);
        if (ObjectUtil.isNull(reservation)) {
            throw new BaseException("预约信息不存在");
        }
        reservation.setStatus(2);
        reservation.setUpdateBy(UserThreadLocal.get().toString());
        reservationMapper.updateById(reservation);
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateReservationStatus() {
        //查询所有待报到且预约时间加半小时小于当前时间的预约
        List<Reservation> list = this.list(new LambdaQueryWrapper<Reservation>()
                .eq(Reservation::getStatus, 0)
                .le(Reservation::getTime, LocalDateTime.now().minusMinutes(30)));
        if (ObjectUtil.isEmpty(list)) {
            return;
        }
        list.forEach(reservation -> {
            reservation.setStatus(3);
            reservation.setUpdateBy("0");
            reservationMapper.updateById(reservation);
        });
    }
}
